package com.maimang.platform.dsp.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.maimang.platform.dsp.constant.AdvertiserApi;
import com.maimang.platform.dsp.entity.Deliver;
import com.maimang.platform.dsp.mapper.DeliverMapper;
import com.maimang.platform.dsp.service.AccessTokenService;
import com.maimang.platform.dsp.service.DeliverService;
import com.maimang.platform.dsp.utils.ApiService;
import com.maimang.platform.lang.ConvertUtils;
import com.maimang.platform.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Rocky
 * @version 1.0.0
 * @classname DeliverServiceImpl
 * @description <br>广告主投放资质服务层实现类</br>
 * @date 2019-12-17 10:11:36
 * @email jintaozhao@qq.com
 */
@Service
public class DeliverServiceImpl implements DeliverService {

	@Autowired
	private ApiService apiService;

	@Autowired
	private AccessTokenService accessTokenService;

	@Autowired
	private DeliverMapper deliverMapper;

	/**
	 * 查询广告主投放资质信息
	 *
	 * @param qualificationId 广告主投放资质ID
	 * @return 广告主投放资质信息
	 */
	@Override
	public Deliver selectDeliverById(Long qualificationId) {
		return deliverMapper.selectDeliverById(qualificationId);
	}

	/**
	 * 查询广告主投放资质列表
	 *
	 * @param deliver 广告主投放资质信息
	 * @return 广告主投放资质集合
	 */
	@Override
	public List<Deliver> selectDeliverList(Deliver deliver) {
		return deliverMapper.selectDeliverList(deliver);
	}

	/**
	 * 新增广告主投放资质
	 *
	 * @param deliver 广告主投放资质信息
	 * @return 结果
	 */
	@Override
	public int insertDeliver(Deliver deliver) {
		return deliverMapper.insertDeliver(deliver);
	}

	/**
	 * 修改广告主投放资质
	 *
	 * @param deliver 广告主投放资质信息
	 * @return 结果
	 */
	@Override
	public int updateDeliver(Deliver deliver) {
		return deliverMapper.updateDeliver(deliver);
	}

	/**
	 * 删除广告主投放资质对象
	 *
	 * @param ids 需要删除的数据ID
	 * @return 结果
	 */
	@Override
	public int deleteDeliverByIds(String ids) {
		return deliverMapper.deleteDeliverByIds(ConvertUtils.toStrArray(ids));
	}

	/**
	 * 同步投放资质信息
	 *
	 * @param advertiserId 代理商广告主id
	 */
	@Override
	public void getQualificationDeliver(Long advertiserId) {
		Map param = new HashMap();
		param.put("advertiser_id", advertiserId);
		String accessToken = accessTokenService.accessToken();
		JSONObject result = apiService.sendGet(AdvertiserApi.DELIVER, accessToken, param);
		if (result != null) {
			if (result.getInteger("code") == 0) {
				JSONObject data = result.getJSONObject("data");

				JSONArray list = data.getJSONArray("list");
				if (list == null) {
					return;
				}
				List<Deliver> deliverList = list.toJavaList(Deliver.class);
				// 先删除旧数据，再插入最新数据
				for (Deliver deliver : deliverList) {
					deliver.setAdvertiserId(advertiserId);
					if (StringUtils.isNotNull(deliver.getQualificationId())) {
						deliverMapper.deleteDeliverById(deliver.getQualificationId());
					}
					deliverMapper.insertDeliver(deliver);
				}
			}
		}
	}

}
